import { Component, type ReactNode } from "react";
import type { State } from "../../type";

export default class StateSetState extends Component {
  state: Readonly<State> = {
    count: 0,
    title: '标题'
  }
  constructor (props: Readonly<unknown>) {
    super(props)
  }
  render(): ReactNode {
    return (
      <>
        <div>{ this.state.count }</div>
        <button onClick={
          () => this.setState({ count: this.state.count + 1 })
        }>按钮，点击+1</button>

        <div>{ this.state.title }</div>
        <button onClick={
          () => this.setState(
            (prevState: Readonly<State>, props) => {
              console.log(prevState)
              console.log(props)
              return {
                title: Math.random()
              }
            },
            () => {
              console.log(this.state)
            }
          )
        }>按钮，点击改变标题</button>
      </>
    )
  }
}
